<template>
  <el-dialog
    :visible.sync="dialogVisible"
    width="800px"
    :before-close="handleClose"
    :close-on-click-modal="false"
    append-to-body
    :title="title"
    center
  >
    <el-form
      :model="form"
      ref="formRef"
      label-width="100px"
      style="max-height: 580px; overflow: hidden; overflow-y: scroll"
    >
      <el-form-item
        label="常用语"
        prop="content"
        :rules="[{ required: true, message: '请输入', trigger: 'blur' }]"
      >
        <el-input placeholder="请输入" v-model="form.content"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="tx-c">
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="confirm" :loading="btnLoading"
        >确认</el-button
      >
    </div>
  </el-dialog>
</template>

<script>
import { debounce } from "@/util";
import {
  ApigetReplyTemplateUpdate,
  ApigetReplyTemplateCreate,
} from "@/api/index.js";
import ImgUploadPath from "@/components/imgUpload-path.vue";
export default {
  components: {
    ImgUploadPath,
  },
  data() {
    return {
      oldform: {
        content: "",
        id: "",
      },
      form: {},
      btnLoading: false,
      dialogVisible: false,
      id: null,
      title: null,
    };
  },

  methods: {
    /*   getUnitList() {
        ApigetUnitList({ page: 1, size: 999 }).then(res => {
          this.unitListArr = res.data
        })
      }, */
    onUploadSuccess(path) {
      this.form.pic = path;
    },
    onUploadRemove() {
      this.form.pic = "";
    },
    confirm: debounce(
      function () {
        console.log(this.id);
        this.$refs["formRef"].validate(async (valid) => {
          if (valid) {
            this.btnLoading = true;
            try {
              if (this.id) {
                await ApigetReplyTemplateUpdate(this.id, this.form);
                this.$message.success("修改成功");
              } else {
                await ApigetReplyTemplateCreate(this.form);
                this.$message.success("添加成功");
                this.$emit("refresh");
                /* this.$alert("添加成功，该员工初始密码为：123456", "提示", {
                  confirmButtonText: "确定",
                  callback: (action) => {},
                }); */
              }

              this.handleClose();
              this.$emit("refresh");
            } finally {
              this.btnLoading = false;
            }
          } else return false;
        });
      },
      500,
      true
    ),

    show(row) {
      const data = row || this.oldform;
      for (const key in this.oldform) {
        this.$set(this.form, key, data[key]);
      }
      this.id = row.id || "";
      if (row.id) {
        this.form.content = row.content;
        this.title = "编辑常用语";
      } else {
        this.title = "新增常用语";
      }
      this.dialogVisible = true;
    },

    handleClose() {
      this.dialogVisible = false;
      this.form = { ...this.oldform };
      this.$nextTick((_) => {
        this.$refs.formRef.clearValidate();
      });
    },
  },
};
</script>

<style></style>
